package com.polaris.live.ui.live.activity.base

import android.app.Activity
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.CallSuper
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.Utils
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.dialog_fragment.MusicPopup
import com.polaris.live.im.sfs.bean.event.ImConnectedEvent
import com.polaris.live.im.sfs.bean.event.ImDisconnectedEvent
import com.polaris.live.im.sfs.core.SmartFoxConnector
import com.polaris.live.im.sfs.core.SmartFoxManager
import com.polaris.live.ui.live.widget.listener.OnLiveNetworkListener
import com.polaris.live.ui.party.music.MusicWindowView
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * BaseLiveActivity
 *
 * @author Created by lucas on 2023/11/24 11:25
 */
abstract class BaseLiveActivity<VM : BaseViewModel, VB : ViewBinding> : BaseVbActivity<VM, VB>(),
    NetworkUtils.OnNetworkStatusChangedListener, OnLiveNetworkListener,
    Utils.OnAppStatusChangedListener {

    @CallSuper
    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)
        //监听网络状态
        NetworkUtils.registerNetworkStatusChangedListener(this)
        AppUtils.registerAppStatusChangedListener(this)
    }

    override fun onConnected(networkType: NetworkUtils.NetworkType) {
        onNetwork(true, SmartFoxManager.isLiveConnected())
    }

    override fun onDisconnected() {
        onNetwork(false, SmartFoxManager.isLiveConnected())
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onImConnected(event: ImConnectedEvent) {
        if (event.type == SmartFoxConnector.CLIENT_LIVE) {
            onNetwork(NetworkUtils.isConnected(), true)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onImDisconnected(event: ImDisconnectedEvent) {
        if (event.type == SmartFoxConnector.CLIENT_LIVE) {
            onNetwork(NetworkUtils.isConnected(), false)
        }
    }

    @CallSuper
    override fun onDestroy() {
        super.onDestroy()

        EventBus.getDefault().unregister(this)
        NetworkUtils.unregisterNetworkStatusChangedListener(this)
        AppUtils.unregisterAppStatusChangedListener(this)
    }

    /**
     * 前台
     */
    override fun onForeground(activity: Activity?) {
        val lastRoom = SmartFoxManager.getLiveRoom()
        //回到前台如果直播间没连接上，但是im链接上了   需要重连一下
        if (lastRoom == null && SmartFoxManager.isLiveConnected()) {
            onNetwork(NetworkUtils.isConnected(), SmartFoxManager.isLiveConnected())
        }
    }

    /**
     * 后台
     */
    override fun onBackground(activity: Activity?) {
    }

    /**
     * 显示音乐控制器
     */
    fun showMusicWindowView() {
        val rootView = window.decorView.findViewById<ViewGroup>(android.R.id.content)
        val lp = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        lp.topMargin = ScreenUtils.getScreenHeight() - 300.autoDp

        lp.gravity = if (AppCodeUtils.isAR()) {
            Gravity.START
        } else {
            Gravity.END
        }
        val view = MusicWindowView(this).apply {
            layoutParams = lp
            setClickMoreListener {
                MusicPopup.show(this@BaseLiveActivity) {
                }
            }
        }
        rootView.addView(view)
    }
}